// =============================================================================
//  STATS - Statistical Analysis Tools, v.0.01
//  Copyright (C) 2011  Marco Vettigli
// -----------------------------------------------------------------------------
//
// This program  is free software:  you can  redistribute it and/or  modify it
// under the terms of the  GNU General Public License as published by the Free
// Software Foundation,  either version 3 of the License,  or (at your option)
// any later version.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY;  without even  the  implied  warranty  of  MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE.  See the  GNU General Public License  for
// more details.
//
// You  should  have received a copy of the  GNU General Public License  along
// with this program.  If not, see <http://www.gnu.org/licenses/>.
//
// == module ===================================================================
//
// SCOLPROPERTIES.CPP
// The class defines a properties of a given row inside STable.
//
// Author: Marco Vettigli
//
// This class id used to store column information (properties) for use in data
// structure of STable objects. These properties are applied to a single array
// but all they doe does not refer to a generic array (such as SDoubles and
// SStrings). These information refer to column data structure, so they are
// kept outside array definitions and used directly inside an STable object.
//
// Properties stored are: array type, array pointer for column localization;
// selection state for table selection managment; description, units, upper
// and lower spec limits as accessors information.
//
// == implementation ===========================================================
//
// All properties are stored as private variable and content accessed by
// getter/setter methods. Array type are defined as elements of the enum
// ArrayType.
//
// =============================================================================

// == include directives =======================================================

#include <QDebug>
#include "scolproperty.h"

// == constructors and destructors =============================================

StatS::SColProperty::SColProperty()
{
    // initialise object properties
    _type = Undefined;
    _array = 0;
    _selectedState = false;
    _description = QString();
    _units = QString();
    _usl = 0;
    _lsl = 0;

}

// == getters ==================================================================

StatS::SColProperty::ArrayType StatS::SColProperty::type() const
{
    return _type;
}

void * StatS::SColProperty::array() const
{
    return _array;
}

QString StatS::SColProperty::description() const
{
    return _description;
}

QString StatS::SColProperty::units() const
{
    return _units;
}

double StatS::SColProperty::usl() const
{
    return _usl;
}

double StatS::SColProperty::lsl() const
{
    return _lsl;
}

QString StatS::SColProperty::getToolTip() const
{
    QString toolTip;
    toolTip.append(_units.isEmpty() ?
                       QString() :
                       QString("(%1)").arg(_units));
    switch (_type) {
    case StatS::SColProperty::SDoubles:
        toolTip.append(QString("(USL=%1, LSL=%2)")
                       .arg(_lsl)
                       .arg(_usl));
    default: break;
    }
    toolTip.append(_description.isEmpty() ?
                       QString() :
                       QString(", %1").arg(_description));
    return toolTip;
}

// == setters ==================================================================

bool StatS::SColProperty::setType(StatS::SColProperty::ArrayType type)
{
    // check if type is valid
    switch (type) {
    case Undefined:
    case SStrings:
    case SDoubles:
        break;
    default:
        qWarning() << "ArrayType not defined: " << type;
        return false;
    }
    // check if new type is different from current one
    if (_type == type) return false;
    // assign new type and return
    _type = type;
    return true;
}

bool StatS::SColProperty::setArray(void *array)
{
    // check if new array is different from current one
    if (_array == array) return false;
    // make the object consistent
    if (array == 0) _type = Undefined;
    // assign new array and return
    _array = array;
    return true;
}

bool StatS::SColProperty::setSelected(bool state)
{
    // check if new selection state is different from current one
    if (_selectedState == state) return false;
    // assign new selection state and return
    _selectedState = state;
    return true;
}

bool StatS::SColProperty::setDescription(const QString description)
{
    // check if new description is different from current one
    if (_description == description) return false;
    // assign new description and return
    _description = description;
    return true;
}

bool StatS::SColProperty::setUnits(const QString units)
{
    // check if new unit is different from current one
    if (_units == units) return false;
    // assign new unit and return
    _units = units;
    return true;
}

bool StatS::SColProperty::setUsl(double usl)
{
    // check if new usl is different from current one
    if (_usl == usl) return false;
    // check object consistency
    if (usl < _lsl) {
        double temp = usl;
        usl = _lsl;
        _lsl = temp;
    }
    // assign new usl and return
    _usl = usl;
    return true;
}

bool StatS::SColProperty::setLsl(double lsl)
{
    // check if new lsl is different from current one
    if (_lsl == lsl) return false;
    // check object consistency
    if (lsl > _usl) {
        double temp = lsl;
        lsl = _usl;
        _usl = temp;
    }
    // assign new lsl and return
    _lsl = lsl;
    return true;
}

// == inspectors ===============================================================

bool StatS::SColProperty::isSelected() const
{
    return _selectedState;
}

// == accessor function definitions ============================================

// == static variables =========================================================

// == static funtion definitions ===============================================

// == private function definitions =============================================
